#ifdef KITSCHY_DEBUG_MEMORY 
#include "debug_memorymanager.h"
#endif

#ifdef WIN32
#include "windows.h"
#endif

#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#include "math.h"

#include <GL/gl.h>
#include "GL/glu.h"

#include <Fl/Fl.H>
#include <FL/Fl_Gl_Window.H>

#include "BList.h"
#include "BVector.h"
#include "quaternion.h"
#include "symbol.h"
#include "sort.h"
#include "ontology.h"
#include "featureterm.h"
#include "FTKBase.h"

#include "myglutaux.h"
#include "CMC.h"
#include "3dobject.h"
#include "AWLocalViewport.h"

#include "AWMessage.h"
#include "AWEntity.h"
#include "AWScenario.h"
#include "AWBody.h"
#include "AWSensor.h"
#include "AWBrain.h"
#include "AWAgent.h"
#include "AWWorld.h"

#include "awwindow.h"


extern	AW_World *World;
extern int SCREEN_DX,SCREEN_DY;


void AWWindow::draw()
{
	if (!valid()) {
		float fAspect;

		/* Recalcular perspectiva: */ 
		glShadeModel(GL_SMOOTH);							// Enable Smooth Shading
		glClearColor(0.0f, 0.0f, 0.0f, 0.5f);				// Black Background
		glClearDepth(1.0f);									// Depth Buffer Setup
		glEnable(GL_DEPTH_TEST);							// Enables Depth Testing
		glEnable(GL_COLOR_MATERIAL);
		glEnable(GL_LIGHTING);
		glDepthFunc(GL_LEQUAL);								// The Type Of Depth Testing To Do
		glPolygonMode(GL_FRONT,GL_FILL);
		glEnable(GL_NORMALIZE);

		SCREEN_DX=w();
		SCREEN_DY=h();

		glViewport(0, 0, (GLsizei) w(), (GLsizei) h());
		
		glMatrixMode(GL_PROJECTION);
		glLoadIdentity();
		if (h()) fAspect = (GLfloat)w()/h();
			else fAspect = 1.0f;
			
		gluPerspective(45.0f, fAspect, 0.1F, 100);

		glMatrixMode(GL_MODELVIEW);
		glLoadIdentity();

	} /* if */ 

	/* Dibujar: */ 
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);	// Clear Screen And Depth Buffer

	if (World!=0) {
		World->beforedraw();
		World->draw();
		World->afterdraw();
	} /* if */ 
	
} /* AWWindow::draw */ 


int AWWindow::handle(int event)
{
	switch(event) {
	case FL_PUSH:
		World->MouseClick(Fl::event_x(),Fl::event_y());
		return 1;
		break;
	case FL_KEYBOARD:
		World->KeyboardEvent(Fl::event_key());
		return 1;
		break;
	default:return 0;
	} /* switch */ 

} /* AWWindow::handle */ 
